package com.moduls.interfaceFunction.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class basictool {
	  public static InetAddress getInetAddress(){  
		  
	        try{  
	            return InetAddress.getLocalHost();  
	        }catch(UnknownHostException e){  
	            System.out.println("unknown host!");  
	        }  
	        return null;  
	  
	    }  
	  
	    public static String getHostIp(){ 
	    	InetAddress netAddress=getInetAddress();
	        if(null == netAddress){  
	            return null;  
	        }  
	        String ip = netAddress.getHostAddress(); //get the ip address  
	        return ip;  
	    }  
	  
	    public static String getHostName(){  
	    	InetAddress netAddress=getInetAddress();
	        if(null == netAddress){  
	            return null;  
	        }  
	        String name = netAddress.getHostName(); //get the host address  
	        return name;  
	    }  
	    /**
	     * 通过递归删除html标签
	     * @param content - 包含HTML标签的内容 
	     * @author Jack, 2014-05-15.
	     * @return 不带HTML标签的文本内容
	     */
	    public static String removeHtmlTag(String content) {
	        Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>");
	        Matcher m = p.matcher(content);
	        if (m.find()) {
	            content = content.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>", "$2");
	            content = removeHtmlTag(content);
	        }
	        return content;
	    }
	    /**
	     * 正则校验身份证是否符合第一代第二代标准
	     * @param cardcode
	     * @return
	     */
	    public boolean cardCodeVerifySimple(String cardcode) {
		    //第一代身份证正则表达式(15位)
		    String isIDCard1 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
		    //第二代身份证正则表达式(18位)
		    String isIDCard2 ="^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z])$";

		    //验证身份证
		    if (cardcode.matches(isIDCard1) || cardcode.matches(isIDCard2)) {
		        return true;
		    }
		    return false;
		}
	    /**
	     * 验证第二代身份证是否符合国家规范
	     * @param cardcode
	     * @return
	     */
		public boolean cardCodeVerify(String cardcode) {
//		    int i = 0;
//		    String r = "error";
//		    String lastnumber = "";
//
//		    i += Integer.parseInt(cardcode.substring(0, 1)) * 7;
//		    i += Integer.parseInt(cardcode.substring(1, 2)) * 9;
//		    i += Integer.parseInt(cardcode.substring(2, 3)) * 10;
//		    i += Integer.parseInt(cardcode.substring(3, 4)) * 5;
//		    i += Integer.parseInt(cardcode.substring(4, 5)) * 8;
//		    i += Integer.parseInt(cardcode.substring(5, 6)) * 4;
//		    i += Integer.parseInt(cardcode.substring(6, 7)) * 2;
//		    i += Integer.parseInt(cardcode.substring(7, 8)) * 1;
//		    i += Integer.parseInt(cardcode.substring(8, 9)) * 6;
//		    i += Integer.parseInt(cardcode.substring(9, 10)) * 3;
//		    i += Integer.parseInt(cardcode.substring(10,11)) * 7;
//		    i += Integer.parseInt(cardcode.substring(11,12)) * 9;
//		    i += Integer.parseInt(cardcode.substring(12,13)) * 10;
//		    i += Integer.parseInt(cardcode.substring(13,14)) * 5;
//		    i += Integer.parseInt(cardcode.substring(14,15)) * 8;
//		    i += Integer.parseInt(cardcode.substring(15,16)) * 4;
//		    i += Integer.parseInt(cardcode.substring(16,17)) * 2;
//		    i = i % 11;
//		    lastnumber =cardcode.substring(17,18);
//		    if (i == 0) {
//		        r = "1";
//		    }
//		    if (i == 1) {
//		        r = "0";
//		    }
//		    if (i == 2) {
//		        r = "x";
//		    }
//		    if (i == 3) {
//		        r = "9";
//		    }
//		    if (i == 4) {
//		        r = "8";
//		    }
//		    if (i == 5) {
//		        r = "7";
//		    }
//		    if (i == 6) {
//		        r = "6";
//		    }
//		    if (i == 7) {
//		        r = "5";
//		    }
//		    if (i == 8) {
//		        r = "4";
//		    }
//		    if (i == 9) {
//		        r = "3";
//		    }
//		    if (i == 10) {
//		        r = "2";
//		    }
//		    if (r.equals(lastnumber.toLowerCase())) {
//		        return true;
//		    }
//		    return false;
			return true;
		}
		
		/**
		 * 通过身份证取生日
		 * @param idcard
		 * @return
		 */
		public String birthday_fromidcard(String idcard) {
			try {
				return idcard.substring(6, 10)+"-"+	idcard.substring(10, 12)+"-"+idcard.substring(12, 14);	
			} catch (Exception e) {
				return "";
			}
			
		}
		
		/**
		 * 对字符串处理:将指定位置到指定位置的字符以星号代替
		 * 
		 * @param content
		 *            传入的字符串
		 * @param begin
		 *            开始位置
		 * @param end
		 *            结束位置
		 * @return
		 */
		public  String string_star(String content, int begin, int end) {

			if (begin >= content.length() || begin < 0) {
				return content;
			}
			if (end >= content.length() || end < 0) {
				return content;
			}
			if (begin >= end) {
				return content;
			}
			String starStr = "";
			for (int i = begin; i < end; i++) {
				starStr = starStr + "*";
			}
			return content.substring(0, begin) + starStr + content.substring(end, content.length());

		}
		
		/**
		 * MD5加密
		 * @param s
		 * @return
		 */
		public String MD5(String s) {
		    try {
		        MessageDigest md = MessageDigest.getInstance("MD5");
		        byte[] bytes = md.digest(s.getBytes("utf-8"));
		        return toHex(bytes);
		    }
		    catch (Exception e) {
		        throw new RuntimeException(e);
		    }
		}

		private static String toHex(byte[] bytes) {

		    final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
		    StringBuilder ret = new StringBuilder(bytes.length * 2);
		    for (int i=0; i<bytes.length; i++) {
		        ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
		        ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
		    }
		    return ret.toString();
		}
		/** 
		    * 获取未来 任意天内的日期数组 
		    * @param intervals      intervals天内 
		    * @return              日期数组 
		    */  
		   public  ArrayList<String> getFetureDate_days(int intervals ) {  
		       ArrayList<String> fetureDaysList = new ArrayList<>();  
		       for (int i = 0; i <intervals; i++) {   
		           fetureDaysList.add(getFetureDate(i));  
		       }  
		       return fetureDaysList;  
		   }  
		   /** 
		    * 获取未来 第 past 天的日期 
		    * @param past 
		    * @return 
		    */  
		   public  String getFetureDate(int past) {  
		       Calendar calendar = Calendar.getInstance();  
		       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);  
		       Date today = calendar.getTime();  
		       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		       String result = format.format(today);   
		       return result;  
		   }  
		
}
